using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	public static class GraphicExtension
	{
		public static bool IsSpriteOrVectorImage(this Graphic graphic)
		{
			return graphic is Image || graphic is VectorImage;
		}

		public static void SetImage(this Graphic graphic, Sprite sprite)
		{
			Image image = graphic as Image;
			if (image != null)
			{
				image.sprite = sprite;
			}
		}

		public static void SetImage(this Graphic graphic, VectorImageData vectorImageData)
		{
			VectorImage vectorImage = graphic as VectorImage;
			if (vectorImage != null)
			{
				vectorImage.vectorImageData = vectorImageData;
			}
		}

		public static void SetImage(this Graphic graphic, ImageData imageData)
		{
			VectorImage vectorImage = graphic as VectorImage;
			if (vectorImage != null && imageData != null)
			{
				if (imageData.imageDataType == ImageDataType.VectorImage)
				{
					vectorImage.vectorImageData = imageData.vectorImageData;
				}
				return;
			}
			Image image = graphic as Image;
			if (image != null && imageData != null && imageData.imageDataType == ImageDataType.Sprite)
			{
				image.sprite = imageData.sprite;
			}
		}

		public static Sprite GetSpriteImage(this Graphic graphic)
		{
			Image image = graphic as Image;
			if (image != null)
			{
				return image.sprite;
			}
			return null;
		}

		public static VectorImageData GetVectorImage(this Graphic graphic)
		{
			VectorImage vectorImage = graphic as VectorImage;
			if (vectorImage != null)
			{
				return vectorImage.vectorImageData;
			}
			return null;
		}

		public static ImageData GetImageData(this Graphic graphic)
		{
			Sprite spriteImage = graphic.GetSpriteImage();
			if (spriteImage != null)
			{
				return new ImageData(spriteImage);
			}
			VectorImageData vectorImage = graphic.GetVectorImage();
			if (vectorImage != null)
			{
				return new ImageData(vectorImage);
			}
			return null;
		}
	}
}
